//
//  CircleView.m
//  轮播图
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 LanOu. All rights reserved.
//

#import "CircleView.h"
#import "CircleCollectionViewCell.h"
#import "DetailViewController.h"
#import "HeadModel.h"

@interface CircleView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic , assign)  BOOL isDrag;
@property (nonatomic , retain)  NSMutableArray *imgArr;
@end

@implementation CircleView

- (void)dealloc{
    [_collectionView release];
    [_dataArr release];
    [_page release];
    [_timer release];
    [_imgArr release];
    [_navi release];
    [super dealloc];
}



- (instancetype)initWithFrame:(CGRect)frame dataArr:(NSArray *)dataArr
{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.itemSize = frame.size;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_collectionView];
        _collectionView.contentOffset = CGPointMake(frame.size.width, 0);
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[CircleCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
        
        [layout release];
        [_collectionView release];
//        NSLog(@"%ld",dataArr.count);
        if (dataArr.count) {
            
            self.dataArr = dataArr;
            [self createTimer];
        }
        
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake((self.frame.size.width - 200) / 2.0, _collectionView.frame.size.height - 40, 200, 40)];
        _page.currentPageIndicatorTintColor = [UIColor whiteColor];
        _page.pageIndicatorTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.3];
        _page.userInteractionEnabled = NO;
        _page.numberOfPages = dataArr.count;
        
        [self addSubview:_page];

    }
    return self;
}

- (void)setDataArr:(NSMutableArray *)dataArr{
    if (_dataArr != dataArr) {
        [_dataArr release];
        _dataArr = [dataArr retain];
    }
    [self dataHandle];
    _page.numberOfPages = dataArr.count;
    [self.collectionView reloadData];
    _collectionView.contentOffset = CGPointMake(self.frame.size.width, 0);
    if (self.timer) {
        [self.timer invalidate];
    }
    [self createTimer];
}

// (如果轮播图需要点击的时候改变一下)
- (void)dataHandle{
    NSMutableArray *tempImgArr = [NSMutableArray array];
    
    for (HeadModel *headM in _dataArr) {
        NSURL *url = [NSURL URLWithString:headM.image];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        [tempImgArr addObject:image];
    }
    
    self.imgArr = [NSMutableArray array];
    [_imgArr addObject:[tempImgArr lastObject]];
    for (UIImage *img in tempImgArr) {
        [_imgArr addObject:img];
    }
    [_imgArr addObject:[tempImgArr firstObject]];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _imgArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CircleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.imgView.image = _imgArr[indexPath.item];
    return cell;
}

#pragma mark - scroll
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (_collectionView.contentOffset.x == (_imgArr.count - 1) * self.frame.size.width) {
        _collectionView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    if (!_isDrag) {
        [self.timer invalidate];
        _isDrag = YES;
    }
}

- (void)createTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(play) userInfo:nil repeats:YES];
}

- (void)dropTimer{
    [self.timer invalidate];
}

- (void)play{
    if (_collectionView.contentOffset.x == (_imgArr.count - 1) * self.frame.size.width) {
        _collectionView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    if (_collectionView.contentOffset.x == 0) {
        _collectionView.contentOffset = CGPointMake((_imgArr.count - 2) * self.frame.size.width, 0);
    }
    NSInteger item = (_collectionView.contentOffset.x / self.frame.size.width) + 1;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
    [_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionRight) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        if (scrollView.contentOffset.x == (_imgArr.count - 1) * self.frame.size.width) {
            scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        }
        if (scrollView.contentOffset.x == 0) {
            scrollView.contentOffset = CGPointMake((_imgArr.count - 2) * self.frame.size.width, 0);
        }
        _page.currentPage = _collectionView.contentOffset.x / self.frame.size.width - 1;
    [self createTimer];
    _isDrag = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        if (_collectionView.contentOffset.x == (_imgArr.count - 1) * self.frame.size.width) {
            _page.currentPage = 0;
        } else{
            _page.currentPage = _collectionView.contentOffset.x / self.frame.size.width - 1;
        }
}

//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//
//}

#pragma mark - 轮播图点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger i = indexPath.item - 1;
    
    if (i == _dataArr.count) {
        i = 0;
    }
    if (i == -1) {
        i = _dataArr.count - 1;
    }
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    HeadModel *headM = _dataArr[indexPath.item];
    detailVC.url = headM.url;
    
    
    [self.navi pushViewController:detailVC animated:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
